import { login, getInfo } from '@/api/user'
import { getActivitieIdApi } from '@/api/activity'
import { getToken, getCookies, setCookies, setToken, removeToken,setActivitiesId } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: getCookies('realName'),
    roles: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_ACTIVITIES: (state, activities) => {
    state.activities = activities
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      let activityId = userInfo.activityId
      let data
      if(userInfo.activityId){
        data = {
          account: userInfo.username.trim(),
          password: userInfo.password,
          activityId: activityId
        }
      }else{
        data = {
          account: userInfo.username.trim(),
          password: userInfo.password,
        }
      }
      login(data).then(res => {
        const token = res
        const hour = new Date().getHours()
        this._vm.$notify({
          title: `${hour < 8 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 18 ? '下午好' : '晚上好'}`,
          message: `欢迎登录！美中系统`,
          type: 'success'
        })
        setToken(token)
        
        commit('SET_TOKEN', token)
        if(activityId){
          setActivitiesId(activityId)
          commit('SET_ACTIVITIES', activityId)
        }
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit }) {
    console.log("commit>>",commit);
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { realName } = response
        if (!realName) reject('验证失败，请重新登录。')
        setCookies('realName', realName)
        commit('SET_NAME', realName)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  }, 
  // 获取用户信息
  getActivitieId({ commit },id) {
    console.log("commit>>",commit);
    return new Promise((resolve, reject) => {
      getActivitieIdApi(id).then(response => {
        console.log("response",response);
        // const { realName } = response
        // if (!realName) reject('验证失败，请重新登录。')
        // setCookies('realName', realName)
        // commit('SET_NAME', realName)
        // resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 退出系统
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // 删除令牌
      removeToken('realName') // 删除令牌
      resetRouter() // 路由重置
      commit('RESET_STATE')
      resolve()
    })
  },

  // 前端 登出
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // 删除令牌
      removeToken('realName')
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

